/*
 * 验证手机号
 */
function checkMobile(value) {
    try {
        var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
        return myreg.test(value);
    } catch (ex) {
        return false;
    }
}

// 判断安卓环境
function isAndroid(){
    return navigator.userAgent.toLowerCase().match(/Android/i) == "android";
}

// 判断ios环境
function isIOS(){
    return !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
}

/*
 * 根据移动端设备分辨率来设置字体 方便使用rem
 */
function appendEmByWidth() {
    var navigatorUserAgent = navigator.userAgent;
    var iPhone = navigatorUserAgent.indexOf("iPhone");
    var one_dpr = 0;
    if (iPhone > -1) {
        var dpr = Number(window.devicePixelRatio),
            one_dpr = 1 / dpr;
    } else {
        var dpr = 1,
            one_dpr = 1;
    }
    var html = document.getElementsByTagName("html");
    let screenW = document.body.clientWidth,
        style = document.createElement('style'),
        size = (screenW / 750) * 20,
        styles = 'html ,body{font-size:' + size + 'px !important;}';
    (document.getElementsByTagName("head")[0] || document.body).appendChild(style);
    html[0].setAttribute("data-dpr", dpr);
    if (style.styleSheet) {
        style.styleSheet.cssText = styles;
    } else {
        style.appendChild(document.createTextNode(styles));
    }
}

/*
 * 获取urlData字符串
 */
function urlData() {
    let url = window.location.search; //获取url中"?"符后的字串 
    let theRequest = new Object();
    if (url.indexOf("?") != -1) {
        let str = url.substr(1);
        let strs = str.split("&");
        for (let i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}

/* 会话缓存   */
function setSessionCache() {
    sessionStorage.setItem(arguments[0], JSON.stringify(arguments[1]));
}

function isSessionCache(key) {
    if (sessionStorage.getItem(key)) {
        return true;
    } else {
        return false;
    }
}

function getSessionCache(key) {
    return JSON.parse(sessionStorage.getItem(key));
}

function deleteSessionCache(key) {
    if (typeof key == "string") {
        $(key.split(',')).each(function() {
            sessionStorage.removeItem(this + '');
        });
    }
}

function clearSessionCache() {
    sessionStorage.clear();
}

/* 本地缓存  */
function setLocalCache() {
    if (localStorage) {
        localStorage.setItem(arguments[0], JSON.stringify(arguments[1]));
    } else {
        setCookie(arguments[0], arguments[1], 7776000)
    }

}

function getLocalCache(key) {
    if (localStorage) {
        return JSON.parse(localStorage.getItem(key));
    } else {
        return getCookie(key)
    }

}

function isLocalCache(key) {
    if (localStorage.getItem(key)) {
        return true;
    } else {
        return false;
    }
}

function deleteLocalCache(key) {
    if (localStorage) {
        localStorage.removeItem(key)
    } else {
        clearCookie(key)
    }
}

//取得cookie
function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';'); //把cookie分割成组
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i]; //取得字符串
        while (c.charAt(0) == ' ') { //判断一下字符串有没有前导空格
            c = c.substring(1, c.length); //有的话，从第二位开始取
        }
        if (c.indexOf(nameEQ) == 0) { //如果含有我们要的name
            return unescape(c.substring(nameEQ.length, c.length)); //解码并截取我们要值
        }
    }
    return false;
}
//清除cookie
function clearCookie(name) {
    setCookie(name, "", -1);
}
//设置cookie
function setCookie(name, value, seconds) {
    seconds = seconds || 0; //seconds有值就直接赋值，没有为0，这个根php不一样。
    var expires = "";
    if (seconds != 0) { //设置cookie生存时间
        var date = new Date();
        date.setTime(date.getTime() + (seconds * 1000));
        expires = "; expires=" + date.toGMTString();
    }
    document.cookie = name + "=" + escape(value) + expires + "; path=/"; //转码并赋值
}

function clearLocalCache() {
    localStorage.clear();
}

function getFloat(number, n) { 
  n = n ? parseInt(n) : 0; 
  if (n <= 0) return Math.round(number); 
  number = Math.round(number * Math.pow(10, n)) / Math.pow(10, n); 
  return number; 
};

// 验证正整数
 function isInt(val) {
    if (!/^\d+$/.test(val)) {
        return false;
    }
    return true;
}
// 验证正小数
 function isDec(val) {
    if (!/^\d+\.\d+$/.test(val)) {
        return false
    }
    return true
}
 function isIntOrDec(val) {
    if (isInt(val) || isDec(val)) {
        return true
    }
    return false
}

// 验证邮箱
 function isEmail(val) {
    if (!/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(val)) {
        return false
    }
    return true;
}

//只能输入数字和字母和汉子
 function isNumStrChinese(val) {
    var re = /^[a-zA-Z0-9\u4e00-\u9fa5]*$/
    if (!re.test(val)) {
        return false;
    } else {
        return true;
    }
}


/* 扩展属性end */

/*导出所有方法*/
export default {
    appendEmByWidth: appendEmByWidth,
    checkMobile: checkMobile,
    setSessionCache: setSessionCache,
    isSessionCache: isSessionCache,
    getSessionCache: getSessionCache,
    deleteSessionCache: deleteSessionCache,
    clearSessionCache: clearSessionCache,
    setLocalCache: setLocalCache,
    getLocalCache: getLocalCache,
    isLocalCache: isLocalCache,
    deleteLocalCache: deleteLocalCache,
    clearLocalCache: clearLocalCache,
    urlData: urlData,
	getFloat: getFloat,
    clearCookie: clearCookie,
    setCookie: setCookie,
    getCookie: getCookie,
    isAndroid: isAndroid,
    isIOS:isIOS,
    isInt:isInt,
    isDec:isDec,
    isIntOrDec:isIntOrDec,
    isNumStrChinese:isNumStrChinese,
    isEmail:isEmail
}